package kr.hkit.powerjava.chapter18;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.BoxLayout;
import java.awt.FlowLayout;

public class JFileChooserTest extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JButton openButton;
	private JButton saveButton;
	private JFileChooser fc;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFileChooserTest frame = new JFileChooserTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFileChooserTest() {
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 212);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		fc = new JFileChooser();

		JLabel label = new JLabel("파일 선택기 컴포넌트 테스트입니다.");
		openButton = new JButton("파일 오픈");
		openButton.addActionListener(this);

		saveButton = new JButton("파일저장");
		saveButton.addActionListener(this);

		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panel.add(label);
		panel.add(openButton);
		panel.add(saveButton);
		contentPane.add(panel, BorderLayout.CENTER);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == openButton) { // 파일 열기
			int retrunVal = fc.showOpenDialog(this);
			if (retrunVal == JFileChooser.APPROVE_OPTION) {
			} else {
			}
		} else if (e.getSource() == saveButton) { // 파일 저장
			int returnVal = fc.showSaveDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {

			} else {

			}
		}
	}
}
